<?php
class Category_model extends CI_Model {
	public function getallCate($lang = 'en') {
		$data = array ();
		$this->db->select ( 'c.id, cl.category_name' );
		$this->db->from ( 'categories c' );
		$this->db->where ( 'c.public', '1' );
		$this->db->where ( 'c.category_id_ref', NULL );
		$this->db->order_by ( 'c.order' );
		$this->db->join ( 'category_languages cl', 'c.id = cl.category_id' );
		$this->db->join ( 'languages l', 'l.id = cl.language_id AND l.code =\'' . $lang . '\'' );
		
		$q = $this->db->get ();
		
		if ($q->num_rows ()) {
			$data = $q->result_array ();
		}
		$q->free_result ();
		return $data;
	}
	public function getallCateChildren($id) {
		$data = array ();
		$this->db->select ( 'c.id, cl.category_name' );
		$this->db->from ( 'categories c' );
		$this->db->where ( 'c.public', '1' );
		$this->db->where ( 'c.category_id_ref', $id );
		$this->db->order_by ( 'c.order' );
		$this->db->join ( 'category_languages cl', 'c.id = cl.category_id' );
		/*$this->db->join ( 'languages l', 'l.id = cl.language_id AND l.code =\'' . $lang . '\'' );*/
		
		$q = $this->db->get ();
		
		if ($q->num_rows ()) {
			$data = $q->result_array ();
		}
		$q->free_result ();
		return $data;
	}
	public function getById($id) {
		/*$q = $this->db->get_where ( 'categories', array ('id' => $id ) );*/
		$this->db->select ( 'cl.category_name,c.image' );
		$this->db->from ( 'categories c' );
		$this->db->where ( 'c.id', $id );
		$this->db->join ( 'category_languages cl', 'c.id = cl.category_id' );
		$q = $this->db->get ();
		$data = $q->row_array ();
		$q->free_result ();
		return $data;
	}
	public function getCateByName($name) {
		$this->db->select ( 'c.image,c.id,cl.category_name' );
		$this->db->from ( 'categories c' );
		$this->db->where ( 'cl.category_name', $name );
		$this->db->where ( 'c.public', '1' );
		$this->db->join ( 'category_languages cl', 'c.id = cl.category_id' );
		$q = $this->db->get ();
		$data = $q->row_array ();
		$q->free_result ();
		return $data;
	}
	
	public function getCategoryByIdRef($id_ref) {
		$this->db->select ( 'c.id, cl.category_name,c.image' );
		$this->db->from ( 'categories c' );
		$this->db->where ( 'c.category_id_ref', $id_ref );
		$this->db->where ( 'c.public', '1' );
		
		$this->db->join ( 'category_languages cl', 'c.id = cl.category_id' );
		$this->db->join ( 'languages l', 'l.id = cl.language_id AND l.code =\'' . 'en' . '\'' );
		$this->db->order_by ( 'c.order' );
		$q = $this->db->get ();
		$data = $q->result_array ();
		$q->free_result ();
		return $data;
	}
	
	public function getCategoryAndProduct($id_ref) {
		
		$data = $this->getCategoryByIdRef ( $id_ref );
		if (sizeof ( $data ) == 0) {
			return NULL;
		}
		
		echo '<ul>';
		foreach ( $data as $c ) {
			$a = str_replace ( " ", "-", $c ['category_name'] );
			echo '<li>' . anchor ( 'product/allproduct/' . $a, $c ['category_name'] );
			//echo $c['category_name'];
			

			echo '<div class="sub">';
			
			$result = $this->getCategoryAndProduct ( $c ['id'] );
			
			if (sizeof ( $result ) == 0) {
				$products = $this->Product_model->getByChildCatId ( $c ['id'] );
				
				if (sizeof ( $products ) > 0) {
					
					echo '<ul>';
					foreach ( $products as $p ) {
						$b = str_replace ( " ", "-", $p ['name'] );
						echo '<li>' . anchor ( 'product/product_detail/' . $b, $p ['name'] ) . '</li>';
					}
					echo '</ul>';
				}
			
			}
			
			echo '</div>';
			
			echo '</li>';
		
		}
		echo '</ul>';
	
	}
	public function getCateDescriptionByCateName($name, $lang) {
		$this->db->select ( 'category_description' );
		$this->db->from ( 'categories c' );
		$this->db->where ( 'public', '1' );
		$this->db->join ( 'category_languages cl', 'c.id = cl.category_id' );
		$this->db->join ( 'languages l', 'l.id = cl.language_id AND l.code =\'' . 'en' . '\'' );
			$q = $this->db->get ();
		$data = $q->row_array ();
		$q->free_result ();
		return $data;
	}

}